package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Note;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class NoteService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public NoteService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds Note to the db.
     * @param note Note to be inserted
     * @return ID of the added Note
     */
    public int addNote(Note note) {
        session.beginTransaction();
        int id = getMaxNoteID() + 1;
        note.setNoteID(id);
        id = (Integer) session.save(note);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of Notes.
     * @return Max ID
     */
    public int getMaxNoteID() {
        String hql = "select max(n.noteID) from Note n";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxnoteid = (Integer) list.get(0);
            return maxnoteid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a Note identified by the ID.
     * @param id of the Note
     * @return Note with the ID given (null if not found)
     */
    public Note getNoteByID(int id) {
        session.beginTransaction();
        Note note = (Note) session.get(Note.class, new Integer(id));
        session.getTransaction().commit();
        return (note);
    }

    /**
     * Retrieves a list of all Notes stored in the db.
     * @return List of all Notes (null if no Notes)
     */
    public List<Note> getAllNotes() {
        session.beginTransaction();
        List<Note> noteList = session.createQuery("from Note").list();
        session.getTransaction().commit();
        return (noteList);
    }

    /**
     * Updates a specified Note identified by ID.
     * @param newNote an updated Note
     */
    public void updateNote(Note newNote) {
        session.beginTransaction();
        Note note = (Note) session.get(Note.class, new Integer(newNote.getNoteID()));
        note.setBody(newNote.getBody());
        note.setDate(newNote.getDate());
        note.setSupplier(newNote.getSupplier());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the Note from db.
     * @param id Note ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteNote(int id) {
        try {
            session.beginTransaction();
            Note note = (Note) session.get(Note.class, new Integer(id));
            session.delete(note);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}